<?php
class smarty_ext_contacts {

	function smarty_ext_contacts() {
		global $setupdata, $mysqlclass, $contactsclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->contactsclass=$contactsclass;
	}
	
	function get_all_product_groups(){
		return $this->contactsclass->get_all_product_groups();
	}
	
	function get_all_types_of_group(){
		return $this->contactsclass->get_all_types_of_group();
	}

	function get_product_group_fields($params){
		if ((isset($params['group']) && (strlen($params['group']) > 0))){
			return $this->contactsclass->get_product_group_fields($params['group']);
		}
	}
	
	function get_product_group_data($params){
		if ((isset($params['group']) && (strlen($params['group']) > 0))){
			$group_info = $this->contactsclass->get_product_group_data($params['group']);
			return $group_info;
		}
	}
	
	function get_all_products_by_group($params){
		if ((isset($params['group_id']) && (strlen($params['group_id']) > 0))){
			if ((isset($params['hide']) && (strlen($params['hide']) > 0))) {
				return $this->contactsclass->get_all_products_by_group($params['group_id'], $params['hide']);
			} else {
				return $this->contactsclass->get_all_products_by_group($params['group_id']);
			}
		}
	}
	
	function get_all_products_by_group_name_only($params){
		if ((isset($params['group_id']) && (strlen($params['group_id']) > 0))){
			if (!isset($params['sort']) || (($params['sort'] != 'down') && ($params['sort'] != 'up'))) $params['sort'] = 'no';
			if ((isset($params['hide']) && (strlen($params['hide']) > 0))) {
				if (isset($params['curent_page']) && (strlen($params['curent_page']) > 0)) {
					return $this->contactsclass->get_all_products_by_group_name_only($params['group_id'], $params['hide'], $params['curent_page'], $params['sort']);
				} else {
					return $this->contactsclass->get_all_products_by_group_name_only($params['group_id'], $params['hide'], 1, $params['sort']);
				}
			} else {
				if (isset($params['curent_page']) && (strlen($params['curent_page']) > 0)) {
					return $this->contactsclass->get_all_products_by_group_name_only($params['group_id'], $params['hide'], $params['curent_page'], $params['sort']);
				} else {
					return $this->contactsclass->get_all_products_by_group_name_only($params['group_id'], "", 1, $params['sort']);
				}
			}
		}
	}
	
	function get_product_fields($params){
		if ((isset($params['product_id'])) && (strlen($params['product_id']) > 0)){
			return $this->contactsclass->get_product_fields($params['product_id']);
		}
	}
	
	function get_group_fields($params){
		if ((isset($params['group_id'])) && (strlen($params['group_id']) > 0)){
			return $this->contactsclass->get_group_fields($params['group_id']);
		}
	}
	
	function get_product_field_data($params){
		return $this->contactsclass->get_product_field_data($params['parent'], $params['name'], $params['lang']);
	}
	
	function get_product_fields_with_data($params){
		if ((isset($params['product_id'])) && (strlen($params['product_id']) > 0)){
			$product_info = $this->contactsclass->get_product_fields_with_data($params['product_id'], $_SESSION['lang']);
			return $product_info;
		}
	}
	
	function get_product_fields_with_data_and_type($params){
		if ((isset($params['product_id'])) && (strlen($params['product_id']) > 0)){
			$product_info = $this->contactsclass->get_product_fields_with_data_and_type($params['product_id'], $_SESSION['lang']);
			return $product_info;
		}
	}
	
	function get_products_by_search($params){
	 	if (!isset($_GET['cache_id'])) $product_cache_id=NULL; else $product_cache_id=$_GET['cache_id'];
		if ($product_cache_id==NULL) if (!isset($_POST['cache_id'])) $product_cache_id=NULL; else $product_cache_id=$_POST['cache_id'];
		if ((isset($params['group_id'])) && (strlen($params['group_id']) > 0)) $group = $params['group_id'];
		else $group = "0";
		if ((isset($params['search'])) && (strlen($params['search']) > 0)) {
			$query[1]['value'] = $params['search'];
		}
		if (isset($query) && (is_array($query)) && (count($query) > 0)) {
			$tmp = $this->contactsclass->products_search($query, "0", $group, 1);
			$data['search']['products'] = $tmp['products'];
			$data['search']['cache_id'] = $tmp['product_search_cache_id'];
			$data['search']['query'] = $query;
		} elseif ($product_cache_id != NULL){
			$tmp = $this->contactsclass->products_search(array(), $product_cache_id, $group, 1);
			$data['search']['products'] = $tmp['products'];
			$data['search']['cache_id'] = $tmp['product_search_cache_id'];
			$data['search']['query'] = $tmp['query'];
		} else $data['search']['products'] = "";
		return $data;
	}
	
	function get_all_modules($params){
		if (!isset($this->setupdata['modules']['contacts']['modules'])) $contacts_modules = array();
		else $contacts_modules = $this->setupdata['modules']['contacts']['modules'];
		if (!isset($params['user']) || ((intval($params['user'])) == 0)) return $contacts_modules;
		global $usersclass;
		$tmp2 = $this->contactsclass->get_all_product_groups();
		$tmp = $contacts_modules;
		$user_info = $usersclass->GetUserInfo($params['user'], "admins");
		if (($params['user'] == 9999999) || ($user_info['mod']['1'] == 1)) {
			if (count($tmp) > 0) foreach($tmp as $key=>$value) $contacts_modules[$key] = 1;
			if (count($tmp2) > 0) foreach($tmp2 as $key=>$value) $contacts_modules['product_groups'][$key] = 1;
			return $contacts_modules;
		} else {
			if (count($tmp) > 0) foreach($tmp as $key=>$value) $contacts_modules[$key] = 0;
			if (count($tmp2) > 0) foreach($tmp2 as $key=>$value) $contacts_modules['product_groups'][$key] = 0;
		}
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id`=".$params['user']." && `module` LIKE 'contacts_module_%'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if ($row['module'] != 'contacts_module_group'){
				$row['module'] = substr($row['module'], strlen("contacts_module_"), strlen($row['module'])-strpos($row['module'], "contacts_module_"));
				if (array_key_exists($row['module'],$contacts_modules)) $contacts_modules[$row['module']] = 1;
			} else {
				$contacts_modules['product_groups'][$row['page_id']] = 1;
			}
		}
		
		if (isset($contacts_modules['product_groups'])){
			foreach($contacts_modules['product_groups'] as $key=>$value){
				if ($value == 1){
					if ($tmp2[$key]['parent'] != 0) $contacts_modules['product_groups'][$tmp2[$key]['parent']] = 1;
				}
			}
		}
		
		return $contacts_modules;
	}
	
	function get_super_select($params){
		return $this->contactsclass->get_super_select($params);
	}
	
	function get_page_id_from_product_group_id($params){
		if (isset($params['parent']) && (strlen($params['parent']) > 0)){
			if  (!isset($params['lang']) || (strlen($params['lang']) == 0)) $params['lang'] = $_SESSION['lang'];
			return $this->contactsclass->get_page_id_from_product_group_id($params['parent'], $params['lang']);
		}
	}
	
	function get_group_count_data($params){
		if (isset($params['main']) && (strlen($params['main']) > 0)) $main = intval($params['main']);
		else $main = 0;
		if (isset($params['sub']) && (strlen($params['sub']) > 0)) $sub = intval($params['sub']);
		else $sub = 0;
		return $this->contactsclass->get_group_count_data($main, $sub);
	}
	
	function get_all_product_groups_one_product_type($params){
		if (isset($params['type']) && (strlen($params['type']) > 0)) {
			if (isset($params['groups']) && is_array($params['groups'])){
				$result = array();
				foreach($params['groups'] as $key=>$value){
					if (($value['parent'] != 0) && ($value['type']==$params['type'])) $result[$key]=$value;
				}
				foreach($result as $key1=>$value1){
					foreach($params['groups'] as $key2=>$value2){
						if ($value1['parent'] == $key2) $result[$key2]=$value2;
					}
				}
				return $result;
			} else return $params['groups'];
		} else return $params['groups'];
	}
	
	function get_text_for_container($params){
		$all = $this->contactsclass->get_all_product_groups();
		$before = array();
		$value = "";
		$t1 = explode(";", $params['value']);
		foreach ($t1 as $v){
			$t2 = explode(":", $v);
			$t3 = explode(",", $t2[1]);
			if (intval($t2[0]) > 0){
				$value .= $all[$t2[0]]['name'].": ";
				foreach ($t3 as $v3){
					if (intval($v3) > 0){
						$value .= $all[$v3]['name'].", ";
					}
				}
				$value = substr($value, 0, strlen($value)-2);
				$value .= "\n";
			}
		}
		return $value;
	}
	
	function get_permisions_container($params){
		global $treeclass;
		$result = array();
		$value = $treeclass->get_field_value($params['parent'], 'contacts_products_group_id');
		$t1 = explode(";", $value);
		foreach ($t1 as $v){
			$t2 = explode(":", $v);
			$t3 = explode(",", $t2[1]);
			if ((intval($t2[0]) > 0) && (intval($t2[0]) == $params['group'])){
				foreach ($t3 as $v3){
					if (intval($v3) > 0){
						$result[$v3] = "on";
					}
				}
			}
		}
		return $result;
	}
	
	function get_manufacture_id($params){
		if (isset($params['group_id']) && (intval($params['group_id']) > 0)) {
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."contacts_catalog` WHERE `parent`=".intval($params['group_id'])." AND `type`=''", __LINE__, __FILE__);
			if (mysql_num_rows($sql) == 1){
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				return $row['id'];
			} else return "0";
		} else return "0";
	}
}
?>